package com.cqndt.site.controller;

import com.cqndt.site.entity.TabAttachment;
import com.cqndt.site.service.back.TabAttachmentService;
import com.cqndt.site.util.FileUpload;
import com.cqndt.site.util.Result;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * Created By marc
 * Date: 2019/2/14  9:51
 * Description:附件上传、附件预览
 */
@Controller
public class AttachmentController {
    @Autowired
    private TabAttachmentService tabAttachmentService;
    //文件路径
    @Value("${attachmentUrl}")
    private String attachmentUrl;
    @Value("${file-ip}")
    private String fileIp;

    @ResponseBody
    @RequestMapping(value = "fileUpload", method = RequestMethod.POST)
    @ApiOperation(value = "附件上传", notes = "附件上传")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "MultipartFile", name = "file", value = "上传附件", defaultValue = "1", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "attachmentType", value = "附件类型（1.图片 2.视频 3.文件 4.）", defaultValue = "1", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "newName", value = "附件别名", defaultValue = "1", required = true, paramType = "query")
    })
    public Result fileUpload(MultipartFile file, String attachmentType,String newName) {
        Result result = FileUpload.fileUpload(file, attachmentType,attachmentUrl,newName,fileIp);
        Map<String,Object> map = ( Map<String,Object>)result.getData();
        TabAttachment tabAttachment = new TabAttachment();
        tabAttachment.setId(map.get("imgId").toString());
        tabAttachment.setAttachmentUrl(map.get("attachmentUrl").toString());
        tabAttachment.setAttachmentName(map.get("attachmentName").toString());
        tabAttachment.setAttachmentType(attachmentType);
        tabAttachmentService.saveTabAttachment(tabAttachment);
        return result;
    }
}
